package com.jh.user.util;

import cn.jh.common.utils.CommonConstants;
import com.jh.user.pojo.AppSlideshow;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommonsUtil {

    @Autowired
    private static Util util;

    private static Logger log = LoggerFactory.getLogger(CommonsUtil.class);

    /**
     * 返回成功信息
     * @return
     */
    public static Map returnSuccess(Object obj){
        Map map = new HashMap();
        map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
        map.put(CommonConstants.RESULT, obj);
        map.put(CommonConstants.RESP_MESSAGE, "成功");
        return map;
    }


    /**
     * 返回成功信息
     * @return
     */
    public static Map returnSuccess(Object obj,String success){
        Map map = new HashMap();
        map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
        map.put(CommonConstants.RESULT, obj);
        map.put(CommonConstants.RESP_MESSAGE, success);
        return map;
    }

    /**
     * 返回成功信息
     * @return
     */
    public static Map returnSuccess(String code,Object obj,String success){
        Map map = new HashMap();
        map.put(CommonConstants.RESP_CODE, code);
        map.put(CommonConstants.RESULT, obj);
        map.put(CommonConstants.RESP_MESSAGE, success);
        return map;
    }


    /**
     * 返回失败信息
     * @param message
     * @return
     */
    public static Map<String, Object> returnError(String message){
        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
        map.put(CommonConstants.RESP_MESSAGE, message);
        return map;
    }

    /**
     * 其他异常信息
     */
    public static Map<String, Object> returnError(String code,String message){
        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstants.RESP_CODE,code);
        map.put(CommonConstants.RESP_MESSAGE, message);
        return map;
    }


    /**
     * 封装公共调用方法
     * @param requestEntity
     * @param urls
     * @param serviceUrl
     * @return
     */
    public static JSONObject crossDomainModule (MultiValueMap<String, String> requestEntity,String urls,String serviceUrl) {
        RestTemplate restTemplate = new RestTemplate();
        URI uri = util.getServiceUrl(serviceUrl , "error url request!");
        String url = uri.toString() + urls;
        String result = restTemplate.postForObject(url, requestEntity, String.class);
        log.info("RESULT================" + result);
        JSONObject jsonObject = JSONObject.fromObject(result);
        JSONObject resultObj = jsonObject.getJSONObject("result");
        return resultObj;
    }


    /**
     * 模块调用方法（post）
     * @param modular 模块
     * @param requestEntity 入参
     * @param url 访问地址
     * @return
     */
    public static JSONObject modularPost(String modular,MultiValueMap<String, String> requestEntity, String url){
        //查询号码归属地
        URI uri = util.getServiceUrl(modular, "error url request!");
        url = uri.toString() + url ;
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.postForObject(url, requestEntity, String.class);
        JSONObject jsonObject = JSONObject.fromObject(result);
        return jsonObject;
    }

    /**
     * 支付交易模块调用公共方法
     * @param requestEntity 入参
     * @param url 访问地址
     * @return
     */
    public static JSONObject paymentGet(String common,MultiValueMap<String, String> requestEntity, String url){
        //查询号码归属地
        URI uri = util.getServiceUrl(common, "error url request!");
        url = uri.toString() + url;
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        JSONObject jsonObject = JSONObject.fromObject(result);
        return jsonObject;
    }

}
